<?php
/**
 * generreqtype batch del file di release
 */
$pathFileInc = trim($argv[1]);
require $pathFileInc;
require $pathFileConfig;
require_once(SERVER_FS_ROOT."/app/_inc/sdao/StructuredDataAccessObject.php");
require_once(SERVER_FS_ROOT."/app/_inc/mix.func.php");
require_once(SERVER_FS_ROOT."/app/util/imex/_inc/importExport.func.php");
require_once(SERVER_FS_ROOT."/app/util/imex/_inc/genRel.func.php");
require_once(SERVER_FS_ROOT."/app/util/imex/_inc/dZip.inc.php");

//elimina file release precedenti
if (file_exists($pathZipOut)) {unlink($pathZipOut);}
sxBatchOut(date('d/m/y')." - Preparreqtype release Gestione meta-base versione $versioneMB");
if (file_exists($pathZipOutMB)) {unlink($pathZipOutMB);}
//genera stringa meta-base.txt
$objSDAO = new SDAO($aMetaConPar);
$sMetaBaseTxt = genMetaBaseTxt($aDataArea,$aModuli);
unset($objSDAO);

$zip = new dZip($pathZipOutMB);
$zip->addContent($sMetaBaseTxt, "sophix_mb/meta-base.txt");
$zip->addContent($versioneMB, "sophix_mb/version");
unset($sMetaBaseTxt);
// genera file release sophix_mb
genRelZipDir($rootDirSophixMB, 'com', $zip, "sophix_mb");
genRelZipDir($rootDirSophixMB, 'mod', $zip, "sophix_mb");
$zip->save();

sxBatchOut( "Generato $pathZipOutMB");

// genera file release sophix
sxBatchOut(date('d/m/y')." - Preparreqtype release Moduli applicativi versione $versione");

if (file_exists($pathZipOut)) {unlink($pathZipOut);}
$zip = new dZip($pathZipOut);
$zip->addDir("sophix");
$zip->addContent($versione, "sophix/version");
genRelZipDir($rootDirSophix, 'com', $zip, "sophix");

if (count($aModuli)==0) {
	genRelZipDir($rootDirSophix, 'mod', $zip, "sophix");
	sxBatchOut( "Inclusi tutti i moduli");
} else {
	foreach($aModuli as $kk => $modulo) {
		genRelZipDir($rootDirSophix, 'mod/'.$modulo, $zip, 'sophix');
		sxBatchOut( "Incluso modulo ".$modulo);
	}
}
$zip->save();

sxBatchOut( "Generato $pathZipOut");
sxBatchOut( "Elaborreqtype terminata correttamente");

exit();

function genRelZipDir($rootDir,$subDir, &$zip, $rootZip) {
	$dirX = $rootDir.'/'.$subDir;
	if (!$dh = opendir($dirX)) {
    	sxBatchOut("Impossibile aprire <$dirX>\n",true);
	}
	while($fileName = readdir($dh)) {
		if ($fileName[0] == '.') {continue;}
		if ($fileName == 'CVS') {continue;}
		if (is_file($rootDir.'/'.$subDir.'/'.$fileName)) {
			//echo $rootDir.$subDir.$fileName."\r\n";
			if (!$zip->addFile($rootDir.'/'.$subDir.'/'.$fileName, $rootZip.'/'.$subDir.'/'.$fileName)) {
				sxBatchOut( "Errore zip - file = ".$rootDir.'/'.$subDir.'/'.$fileName,true);
			}
			continue;
		}
		genRelZipDir($rootDir, $subDir."/".$fileName, $zip, $rootZip);
	}
}
	
?>
